<?php
/**
* This page is used to create a new page.
*
* @package folio
* @param string $page The page title for the new page.
* @param string $user The user/community to whom this page belongs.
**/

    define("context", "folio_page_create");

    require_once("../../../includes.php");
    require_once("../view/NewPage.php");
    require_once("../model/page_template.php");
    require_once("../model/user.php");
    require_once("../redirect.php");

    global $CFG;

    // Retrieve page name & user name.
    $username = folio_clean(required_param('user'));
    $url = url;

    $user = folio_user::SelectWhereUsername( $username );
    if ( !$user )
        trigger_error('Invalid username ' . $username . ' passed to new.php.', E_USER_ERROR);

    // set the current user.
    global $page_owner;
    $page_owner = $user->ident;

    if ( folio_page::InsertPermission( $user ) ) {
        // Success, we have permission.

        $title = __gettext('Build Page from Template');

        // Build html control
        $form =  new folio_SelectTemplate( $user );
        $form = $form->toString();

        $redirect = new folio_redirect;
        $redirect->redirect_url = "${url}{$username}/page/";
        $redirect = $redirect->html();

        // Build the html controls
        $body = <<< END
        <img src="{$CFG->wwwroot}mod/folio/image/page_new.png"/><br>
        <form method="post" name="elggform" action="{$url}mod/folio/ajax/page_insert.php">
            <br/>
            {$form}
            <br/>
            {$redirect}
            <input type="hidden" name="page_user_ident" value="{$user->ident}" />
            <input type="submit" value="Create Portfolio" />

        </form>
END;

    } else {
       // Failure, we do not have permission to create this page.
       $title = __gettext('You do not have permission to create this page');
       $body = $title;
    }

    header("Cache-control: private");
    $body = templates_draw(array(
                    'context' => 'contentholder',
                    'title' => $title,
                    'body' => $body
                )
                );

    echo templates_page_draw(array(
                    $title, $body
                )
                );
?>
